package com.lumis.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.lumis.dto.Lumi;

@Repository
@Transactional
public class LumisDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public List<Lumi> getAllLumis() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Lumi.class);
		criteria.addOrder(Order.desc("id"));
		sessionFactory.close();
		return criteria.list();
	}
	
	public int altaLumi(Lumi lumi) {
		Integer operacion = (Integer) sessionFactory.getCurrentSession().save(lumi);
		sessionFactory.close();
		return operacion;
	}

	public Integer getNextId() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Lumi.class).setProjection(Projections.max("id"));
		Integer maxId = (Integer)criteria.uniqueResult();
		
		sessionFactory.close();
		
		if(maxId != null)
			return maxId + 1;
		else
			return 1;
	}

	public Lumi getById(int id) {
		Lumi lumi = (Lumi) sessionFactory.getCurrentSession().get(Lumi.class, id);
		sessionFactory.close();
		return lumi;
	}

	public void borrarLumiPorId(int id) {
		String hql = "delete from Lumi where id= :id";
		sessionFactory.getCurrentSession().createQuery(hql).setInteger("id", id).executeUpdate();
		sessionFactory.close();
	}

	public void actualizarLumi(Lumi lumi) {
		Query q = sessionFactory.getCurrentSession().createQuery("from Lumi where id = :id ");
		q.setParameter("id", lumi.getId());
		sessionFactory.getCurrentSession().update(lumi);
		sessionFactory.close();
	}

	public List<Lumi> buscarLumis(Lumi lumi) {

		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Lumi.class);
		
		if(lumi.getPrecioHoraMin() != 0 && lumi.getPrecioHoraMax() != 0){
			criteria.add(Restrictions.between("preciounahora", lumi.getPrecioHoraMin(), lumi.getPrecioHoraMax()));
		}
		if(lumi.getPesoMin() != 0 && lumi.getPesoMax() != 0){
			criteria.add(Restrictions.between("peso", lumi.getPesoMin(), lumi.getPesoMax()));
		}
		if(lumi.getAnoNacimientoMin() != 0 && lumi.getAnoNacimientoMax() != 0){
			criteria.add(Restrictions.between("anoNacimiento", lumi.getAnoNacimientoMax(), lumi.getAnoNacimientoMin()));
		}
		if(lumi.getCodProvincia() != null && !"".equals(lumi.getCodProvincia())){
			criteria.add(Restrictions.like("codProvincia", lumi.getCodProvincia()));
		}
		if(lumi.getCodMunicipio() != null && !"".equals(lumi.getCodMunicipio())){
			criteria.add(Restrictions.like("codMunicipio", lumi.getCodMunicipio()));
		}
		if(lumi.getNacionalidad() != null && !"".equals(lumi.getNacionalidad())){
			criteria.add(Restrictions.like("nacionalidad", lumi.getNacionalidad()));
		}
		if(lumi.getaDomicilio() != null && lumi.getaDomicilio()){
			criteria.add(Restrictions.like("aDomicilio", lumi.getaDomicilio()));
		}
		if(lumi.getApartamentoPropio() != null && lumi.getApartamentoPropio()){
			criteria.add(Restrictions.like("apartamentoPropio", lumi.getApartamentoPropio()));
		}
		if(lumi.getHoteles() != null && lumi.getHoteles()){
			criteria.add(Restrictions.like("hoteles", lumi.getHoteles()));
		}
		if(lumi.getMasajes() != null && lumi.getMasajes()){
			criteria.add(Restrictions.like("masajes", lumi.getMasajes()));
		}
		if(lumi.getStriptease() != null && lumi.getStriptease()){
			criteria.add(Restrictions.like("striptease", lumi.getStriptease()));
		}
		if(lumi.getJuegos() != null && lumi.getJuegos()){
			criteria.add(Restrictions.like("juegos", lumi.getJuegos()));
		}
		if(lumi.getFrancesNatural() != null && lumi.getFrancesNatural()){
			criteria.add(Restrictions.like("francesNatural", lumi.getFrancesNatural()));
		}
		if(lumi.getAnalDepTamano() != null && lumi.getAnalDepTamano()){
			criteria.add(Restrictions.like("analDepTamano", lumi.getAnalDepTamano()));
		}
		if(lumi.getAnalSinImpTamano() != null && lumi.getAnalSinImpTamano()){
			criteria.add(Restrictions.like("analSinImpTamano", lumi.getAnalSinImpTamano()));
		}
		if(lumi.getSado() != null && lumi.getSado()){
			criteria.add(Restrictions.like("sado", lumi.getSado()));
		}
		
		criteria.addOrder(Order.desc("id"));
		
		@SuppressWarnings("unchecked")
		List<Lumi> lumis = criteria.list();
		
		sessionFactory.close();
		return lumis;
	}

	public void actualizarFotosLumi(Lumi lumi) {
		Query q = sessionFactory.getCurrentSession().createQuery("from Lumi where id = :id ");
		q.setParameter("id", lumi.getId());
		Lumi lumidb = (Lumi)q.list().get(0);
		lumidb.setExisteFoto1(lumi.getExisteFoto1());
		lumidb.setExisteFoto2(lumi.getExisteFoto2());
		lumidb.setExisteFoto3(lumi.getExisteFoto3());
		lumidb.setExisteFoto4(lumi.getExisteFoto4());
		lumidb.setExisteFoto5(lumi.getExisteFoto5());
		lumidb.setExisteFoto6(lumi.getExisteFoto6());
		lumidb.setExisteFoto7(lumi.getExisteFoto7());
		lumidb.setExisteFoto8(lumi.getExisteFoto8());
		sessionFactory.getCurrentSession().update(lumidb);
		sessionFactory.close();
	}
}
